/**
 * Created by Administrator on 2016/11/2.
 */
(function(){
    var invitePage={
        init:function(){
            this.datepicker();
            this.collection();
            this.check_respond();
            this.respond_add();
        },
        datepicker:function(){/*日期*/
            $('#input-daterange').datepicker({
                keyboardNavigation: false,
                forceParse: false,
                autoclose: true,
                startDate: 'today'
            });
        },
        collection:function(){  /*收藏*/
            $(".con2_bot .a2").click(function(){ 
                var this_span = $(this);
                var coll_id = $(this).data("inviteid");
                var coll_name = $(this).data("product_name");
                $.post(get_baseUrl()+'Cinvite/user_collect',{coll_id:coll_id,coll_name:coll_name},function(json){
                    if(json.uid) {
                        if(json.collect) {
                            this_span.removeClass("active").html("已收藏");
                            layer.msg("收藏成功",{icon:1,time:1000});
                        }else{
                            this_span.addClass("active").html("收藏");
                            layer.msg("删除收藏成功",{icon:1,time:1000});
                        }
                    }else{
                        layer.msg("请登录",{icon:7,time:1000}, function(){
                        	window.location.href = get_loginUrl();
                        });
                    }
                },'json');
            });
        },
        check_respond:function(){	/*检验报价，弹框*/
        	$(".con2_bot").on("click", "a",function(){
        		var inviteid = $(this).data("inviteid");
        		if($(this).data('status') == 1){
        			$.post(get_baseUrl()+'Crespond/add',{inviteid:inviteid},function(json){
            			if(json.status == false){
            				layer.msg(json.errmsg, {icon:2,time:1000}, function(){
            					if(json.data){window.location.href=json.data;}
            				});
            			}else{
            				$(".Pop_ups").show();
            			}
            		},'json');
        		}
        	});
        	$(".Pop_ups").on("click", ".close", function(){
        		$(".Pop_ups").hide();
        	});
        },
        respond_add:function(){		/*新增报价*/
        	if($("#respond_add").size() > 0){
        		$('form#respond_add').submit(function(e) {
        			var num = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
            		if(!num.test($("input[name=sum_cost]").val())){ 
    			    	layer.msg("整体价格 请输入数字", {icon:2,time:1000});
    			        return false;
    			    }
            		if($("textarea[name=respond_info]").val().length == 0){
            			layer.msg("报价描述 必填", {icon:2,time:1000});
    			        return false;
            		}
            		if(!$("input[name=delivery_time]").val()){
            			layer.msg("交货时间 必填", {icon:2,time:1000});
    			        return false;
            		}
        			$.ajax({
    			        type:"POST",
    			        dataType:"json",
    			        async: false,
                        url: get_baseUrl()+"Crespond/addPost",
                        data: $("form#respond_add").serialize(),
                        beforeSend: function() {
                            $("#respond_add input[type=submit]").val("正在报价...").attr("disabled", true);
                        },
    			        success: function(json) {
                            if (json.status) {
                            	layer.msg("操作成功,在刷新中",function(){
                            		if(json.data){window.location.href = json.data;}
                            	});
                            } else {
                                $("#respond_add input[type=submit]").val("确认报价").removeAttr("disabled");
                                layer.msg(json.errmsg, {icon:2,time:1000}, function(){
                                	if(json.data){window.location.href = json.data;}
                                });
                            }
                        }
    			    });
        			
                    e.preventDefault();
                });
        	}
        },
        
    };
    /*图片滚动切换*/

    invitePage.init();


    function scrollPic(obj){
        function Init(obj){
            this.smallWrap=obj.smallWrap;    //小图片
            this.middleWrap=obj.middleWrap;  //中等图片
            this.bigWrap=obj.bigWrap;         //大图片
            this.prevBtn=obj.prevBtn;      //上一张
            this.nextBtn=obj.nextBtn;      //下一张
            this.closeBtn=obj.closeBtn;   //关闭

            this.showPic=2;      //小图展示个数
            this.n=0;
            this.num=0;
            this.popNum=0;
            window.dir=0;
            this.iT=false;

            this.defineValue();
        }

        Init.prototype={
            constructor:Init,
            defineValue:function(){
                this.bigLi=this.bigWrap.find("li");    //获取大图片li
                this.middleUl=this.middleWrap.find("ul");    //获取中等图片ul
                this.middleLi=this.middleWrap.find("li");    //获取中等图片li
                this.smallUl=this.smallWrap.find("ul");      //获取小图ul
                this.smallLi=this.smallWrap.find("li");      //获取小图li

                this.bigPic=this.bigWrap.find("img");        //获取大图


                this.iLen=this.middleLi.length;               //获取个数
                this.middleLiWidth=this.middleLi.eq(0).outerWidth(true);  //获取中等单张图片的宽度
                this.smallLiWidth=this.smallLi.eq(0).outerWidth(true);    //获取小图单张的宽度

                this.middleUl.width(this.middleLiWidth*this.iLen);        //获取中等图片ul的宽度
                this.smallUl.width(this.smallLiWidth*this.iLen);          //获取小图片ul的宽度
            },
            doEvent:function(){    //调用事件
                this.getPicWidth();
                this.smallPicEvent(this);    //小图片点击事件调用
                this.middleEvent(this);      //中等图片点击事件调用
                this.closeEvent(this);       //弹窗关闭事件
                if(this.iLen>1){
                    this.prevBtn.show();
                    this.nextBtn.show();      //切换按钮显示
                    this.prevEvent(this);     //上一张
                    this.nextEvent(this);    //下一张
                }
                this.popLiEvent(this);     //点击大图让小图与中等图片跳转到当前位置

            },
            getPicWidth:function(){
                var that=this;
                this.bigPic.each(function(index){
                    var _this=$(this);
                    var img=new Image(), maxW= 0, w= 0;
                    img.src=$(this).attr("src");
                    img.onload=function(){
                        _this.css({"margin-left":0,"margin-top":0});

                        if(img.height>500){
                            maxW=Math.floor(500*img.width/img.height);
                            w=maxW>600 ? 600 : maxW;
                            _this.css({"height":500,"width":w,"left":"50%","margin-left":-w/2,"top":0});

                        }else{
                            _this.css({"width":600,"height":500,"left":0,"top":0});
                        }
                        that.domScroll(that);
                    };
                });
            },
            smallPicEvent:function(that){     //小图片事件

                this.smallLi.on("click",function(){   //点击小图片切换事件
                    that.n=$(this).index();
                    that.smallPicShow(that.n);
                    that.bigPicShow(that.n);
                }).eq(this.num).trigger("click");
            },
            smallPicShow:function(n){    //小图滚动封装
                this._num=n-this.num-1;
                this.smallPicMove=this.smallLiWidth*this._num;
                this.smallLi.eq(n).addClass("active").siblings().removeClass("active");
                if(this.iLen>this.showPic){
                    if(n<2){
                        this.smallPicMove=0;
                    }
                    if(this.num==this.iLen-1){
                        this.smallPicMove=-(this._num-1)*this.iLen;
                    }
                    this.smallUl.stop(true).animate({'left':-this.smallPicMove},500);
                }
            },
            bigPicShow:function(n){   //中等图片滚动切换封装
                this.bigPicMove=-n*this.middleLiWidth;
                this.middleUl.stop(true).animate({'left':this.bigPicMove},300);

            },
            middleEvent:function(that){     //点击中等图片显示大图
                var _this=this;
                this.middleLi.on("click",function(){
                    $("html,body").css("overflow-y","hidden");
                    that.bigWrap.show();
                    that.popNum=$(this).index();
                    that.bigBoxShow();
                });
            },
            closeEvent:function(that){
                this.closeBtn.on("click",function(){
                    that.bigWrap.hide();
                    $("html,body").css("overflow-y","auto");
                });
            },
            prevEvent:function(that){
                this.prevBtn.on("click",function(){
                    that.popNum--;
                    if(that.popNum<0){
                        that.popNum=that.iLen-1;
                    }
                    that.bigBoxShow();
                });
                return false;
            },
            nextEvent:function(that){
                this.nextBtn.on("click",function(){
                    that.popNum++;
                    if(that.popNum>=that.iLen){
                        that.popNum=0;
                    }
                    that.bigBoxShow();
                });
                return false;
            },
            bigBoxShow:function(){
                this.bigLi.hide().eq(this.popNum).show();
                dir=0;
                this.getPicWidth();
            },
            popLiEvent:function(that){
                this.bigLi.on("click",function(){
                    that.smallPicShow($(this).index());
                    that.bigPicShow($(this).index());
                    return false;
                });
            },
            domScroll:function(that){

                for(var i=0;i<this.iLen;i++){
                    (function(i){
                        var w=that.bigPic.eq(i).width(),
                            h=that.bigPic.eq(i).height(),
                            l=parseFloat(that.bigPic.eq(i).css("margin-left")),
                            t=parseFloat(that.bigPic.eq(i).css("margin-top"));

                        if(document.addEventListener){
                            that.bigPic.get(i).addEventListener('DOMMouseScroll',function(e){
                                that.scrollFunc(e,$(this),w,h,l,t);
                            },false);
                        }
                        that.bigPic.get(i).onmousewheel=function(e){
                            that.scrollFunc(e,$(this),w,h,l,t);
                        };//IE/Opera/Chrome/Safari
                    })(i);
                }
            },
            scrollFunc:function(e,obj,w,h,l,t){
                var ev=e||window.event;
                if(e.wheelDelta){  //判断ie,google滚轮事件
                    if(e.wheelDelta>0){//向上
                        dir+=30;
                    }
                    if(e.wheelDelta<0){//向下
                        dir-=30;
                    }
                }else if(e.detail){//firefox滚轮事件
                    if(e.detail>0) {//向上
                        dir+=30;
                    }
                    if(e.detail<0){//向下
                        dir-=30;
                    }
                }
                //var scale=1+dir;
                if(dir<=0){
                    dir=0;
                }
                if(dir>=1200){
                    dir=1200;
                }

                obj.css({"width":w+dir,"height":h+dir,"margin-left":-dir/2+l,"margin-top":-dir/2+t});
                this.picMove(obj,-dir/2);
            },
            picMove:function(obj,l){
                var that=this;
                obj.on("mousedown",function(e){
                    e.preventDefault();
                    that.iT=true;
                    var disX= e.pageX-$(this).position().left-$(".pop_pic_con").offset().left; //+parseFloat(img.css("margin-left"))
                    var disY= e.pageY-$(this).position().top-$(".pop_pic_con").offset().top;
                    var _that=$(this);

                    $(document).on("mousemove",function(e){
                        var iLeft= e.pageX-disX-$(".pop_pic_con").offset().left;
                        var iTop=e.pageY-disY-$(".pop_pic_con").offset().top;
                        /*console.log(iLeft);
                         if(-l<iLeft){
                         iLeft=-l;
                         }else if(l>iLeft){
                         iLeft=l;
                         }

                         if(-l<iTop){
                         iTop=-l;
                         }else if(l>iTop){
                         iTop=l;
                         }

                         console.log(l);*/

                        obj.css({"left":iLeft,"top":iTop});
                    });
                    $(document).on("mouseup",function(){
                        $(document).off();
                    });
                    return false;
                });
            }
        };
        return new Init(obj).doEvent();
    }

/*====================调用=======================*/
    scrollPic({
        smallWrap:$(".con2_smallPic"),
        middleWrap:$(".con2_bigPic"),
        bigWrap:$(".pop_Pic"),
        prevBtn:$(".p_prev"),
        nextBtn:$(".p_next"),
        closeBtn:$(".p_close")
    });


})();